/**
 * 
 */
package com.sx.form.ts.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;
import com.sx.article.biz.IAttachmentBiz;
import com.sx.article.entity.AttachmentEntity;
import com.sx.cms.biz.IRegionBiz;
import com.sx.cms.entity.RegionEntity;
import com.sx.cms.entity.UserInfoEntity;
import com.sx.core.BaseAction;
import com.sx.core.StringUtil;
import com.sx.feesunit.exception.SXException;
import com.sx.form.ts.biz.IHouseInfoBiz;
import com.sx.form.ts.entity.HouseInfoEntity;
import com.sx.rbac.entity.SysUserEntity;
import com.sx.util.date.DateUtil;

/**
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
public class HouseInfoAction extends BaseAction implements ModelDriven<HouseInfoEntity>{
	private IHouseInfoBiz houseinfobiz;
	private IAttachmentBiz attachmentbiz;
	private IRegionBiz regionbiz;
	private Log log = LogFactory.getLog(this.getClass());
	private HouseInfoEntity houseinfo = new HouseInfoEntity();
	private String filepath;
	/**
	 * 按照编号给前台获取房源信息
	 **/
	public void findHouseInfoForHtmlLi() throws SXException{
		List<String> list = new ArrayList<String>();
		try {
			StringBuffer buffer = new StringBuffer();
			List<HouseInfoEntity> houseinfos = this.houseinfobiz.findHouseInfoForInfosPage(houseinfo);
			for (HouseInfoEntity hi : houseinfos) {
				String str = "<li city=\""+hi.getCity()+"\" province=\""+hi.getProvince()+"\"><a href=\"http://www.liuxiaofan.com/\"><img src=\""+hi.getValuepath()+"/"+hi.getValuename()+"\"></a><h3>图片标题</h3></li>";
				buffer.append(str);
			}
			outString(buffer.toString());
		} catch (Exception e) {
			// TODO: handle exception
			log.error("按照编号给前台获取房源信息",e);
			throw new SXException();
		}
	}
	/**
	 *按照编号审核房源信息
	 **/
	public void approvalHouseInfoById() throws SXException{
		try {
			houseinfo.setApprovedate(DateUtil.getToday());
			houseinfo.setStauts(new Integer(2));
			SysUserEntity user = super.getUserFromSession();
			houseinfo.setApproverid(user.getUid());
			boolean flag = houseinfobiz.editHouseInfoOnly(houseinfo);
			if(flag){
				outJsonString("{success:true}");
			}else{
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			// TODO: handle exception
			outJsonString("{success:false}");
			log.error("审核房源信息",e);
			throw new SXException();
		}
	}
	/***
	 *按照编号删除房源信息
	 **/
	public void deleteHouseInfoById() throws SXException{
		try {
			boolean flag = houseinfobiz.deleteHouseInfoById(houseinfo);
			if (flag) {
				outJsonString("{success:true}");
			} else {
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			// TODO: handle exception
			outJsonString("{success:false}");
			log.error("删除房源信息",e);
			throw new SXException();
		}
	}
	/**
	 * 获取待审核的房源信息
	 **/
	public void findHouseInfosForApprovaled() throws SXException{
		String str = "{totalCount:0,data:[]}";
		try {
			String queryinfo = "HouseInfo.findHouseInfoApproval";
			List<HouseInfoEntity> list=houseinfobiz.findHouseInfo(houseinfo,queryinfo);
			Integer count = houseinfobiz.findHouseInfoCount(houseinfo);
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("totalCount", count);
			map.put("data", list);
			str=JSONArray.toJSONString(map);
		} catch (Exception e) {
			log.error("获取待审核的房源信息",e);
			throw new SXException();
		}finally{
			outJsonString(str);
		}
	}
	/**
	 *删除文件信息 
	 **/
	public void deleteFileInfo() throws SXException{
		try {
			AttachmentEntity attachment = attachmentbiz.findAttachmentInfoById(houseinfo.getId());
			String filepath = attachment.getFvaluepath()+"/"+attachment.getFvaluename();
			System.out.println("====="+filepath+"====");
			String path = super.getRealyPath(filepath.replaceAll("/sx", ""));
			boolean flag = super.deleteFileInfo(path);
			if (flag) {
				//删除房源与表单数据关联信息
				attachmentbiz.deleteAttachment(attachment);
				houseinfobiz.deleteHouseInfoAndAttachmentInfo(attachment.getFid());
				houseinfobiz.deleteHouseInfoAndKeyImageInfo(attachment.getFid());
				System.out.println(attachment.getFid()+"===");
				outJsonString("{\"success\":true}");
			} else {
				outJsonString("{\"success\":false}");
			}
		} catch (Exception e) {
			outJsonString("{\"success\":false}");
			log.error("删除附件信息",e);
			throw new SXException();
		}
	}
	/**
	 * 根据编号获取表单信息
	 **/
	public void loadInfoById() throws SXException{
		HashMap<String,Object> map = new HashMap<String,Object>();
		try {
			HouseInfoEntity house = houseinfobiz.getHouseInfoById(houseinfo);
			if(house!=null){
				map.put("success", true);
				if(house.getContent()!=null){
					String content =  StringUtil.formatStringToHTML(house.getContent());
					house.setContent(content);
				}
				map.put("data", house);
			}else{
				map.put("success", false);
			}
			String str = JSONObject.toJSONString(map);
			outJsonString(str);
		} catch (Exception e) {
			// TODO: handle exception
			outJsonString("{\"success\":false}");
			log.error("根据编号获取表单信息",e);
			throw new SXException();
		}
	}
	/**
	 *跳转到houseinfo
	 *编辑数据信息 
	 **/
	public String goToEditHouseInfo() throws SXException{
		String page =  "editinfo";
		try {
			page = "editinfo";
			if(houseinfo.getId()!=null){
				getRequest().setAttribute("houseinfo", houseinfo);
			}
		} catch (Exception e) {
			page = ERROR;
			log.error("跳转到编辑房源信息页面",e);
			throw new SXException();
		}
		return page;
	}
	/**
	 * 获取房源数据信息
	 * 用滚动条进行分页
	 **/
	public void queryHouseInfoWithOutTotal() throws SXException{
		String result = "";
		try {
			result = this.houseinfobiz.queryHouseInfoWithOutTotal(houseinfo);
			outJsonString(result);
		} catch (Exception e) {
			outJsonString(result);
			log.error("获取房源数据信息用滚动条进行分页",e);
			throw new SXException();
		}
	}
	
	/**
	 * 按用户编号获取编号信息
	 * 带分页信息
	 **/
	public void queryHouseInfoByUserId() throws SXException{
		String result = "";
		try {
			Integer page = Integer.parseInt(getRequest().getParameter("page"));
			houseinfo.setStart((page-1)*houseinfo.getLimit());
			UserInfoEntity userinfo = super.getUserInfoFromSession();
			houseinfo.setAuthorid(2);
			System.out.println(houseinfo.getStauts());
//			houseinfo.setAuthorid(userinfo.getUserid());
			result = this.houseinfobiz.queryHouseInfoById(houseinfo);
			outJsonString(result);
		} catch (Exception e) {
			outJsonString(result);
			log.error("按用户编号查询表单信息带分页信息",e);
			throw new SXException();
		}
	}
	
	
	/**
	 * 根据区域编号获取数据信息
	 **/
	public void getRegionInfo(){
		try {
			RegionEntity region = new RegionEntity();
			String code = getRequest().getParameter("code");
			String parentid = getRequest().getParameter("parentid");
			if(parentid!=null){
				Integer id = Integer.parseInt(parentid);
				region.setParentid(id);
			}
			region.setCode(code);
			List<RegionEntity> list = regionbiz.findRegionInfo(region);
			String data = JSONArray.toJSONString(list);
			outJsonString(data);
		} catch (Exception e) {
			outJsonString("[]");
			log.error("根据区域编号获取数据信息",e);
		}
	}
	
	/**
	 *上传附件信息
	 **/
	public void uploadImage(){
		try {
			//1.按照文章编号去数据库中检查是否存在编号数量,如果已经5个不上传直接提示
			//2.如果不存在5开始上传
			//2.1上传成功后返回附件编号
			//2.2不成功返回success:false,前端接收到后不加载信息
			String filename = houseinfo.getFiledataFileName();
			File file = houseinfo.getFiledata();
			AttachmentEntity attachment = super.getUpdateFileInfo(getFilepath(), filename, file);
			Integer aid = attachmentbiz.addAttachment(attachment);
			/*Integer id = houseinfo.getId();
			houseinfobiz.addHouseInfoCascadeFileIdInfo(id, aid);*/
			outJsonString("{\"success\":\"true\",\"fileid\":\""+aid+"\",\"filepath\":\""+attachment.getFvaluepath()+"/"+attachment.getFvaluename()+"\"}");
		} catch (Exception e) {
			log.error("上传附件信息",e);
			// TODO: handle exception
		}
	}
	/**
	 *编辑房源信息 
	 **/
	public void editHouseInfo(){
		String result = "{success:true}";
		try {
			houseinfo.setAuthordate(DateUtil.getToday());
			boolean flag = houseinfobiz.editHouseInfo(houseinfo);
			if (flag) {
				result = "{\"success\":true}";
			} else {
				result = "{\"success\":false}";
			}
		} catch (Exception e) {
			result = "{\"success\":false}";
			log.error("编辑房源信息 ",e);
		}
		outJsonString(result);
	}
	
	/**
	 * @return the houseinfobiz
	 */
	public IHouseInfoBiz getHouseinfobiz() {
		return houseinfobiz;
	}

	/**
	 * @param houseinfobiz the houseinfobiz to set
	 */
	public void setHouseinfobiz(IHouseInfoBiz houseinfobiz) {
		this.houseinfobiz = houseinfobiz;
	}
	public HouseInfoEntity getModel() {
		// TODO Auto-generated method stub
		return houseinfo;
	}
	/**
	 * @return the filepath
	 */
	public String getFilepath() {
		return filepath;
	}
	/**
	 * @param filepath the filepath to set
	 */
	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}
	/**
	 * @return the attachmentbiz
	 */
	public IAttachmentBiz getAttachmentbiz() {
		return attachmentbiz;
	}
	/**
	 * @param attachmentbiz the attachmentbiz to set
	 */
	public void setAttachmentbiz(IAttachmentBiz attachmentbiz) {
		this.attachmentbiz = attachmentbiz;
	}
	/**
	 * @return the regionbiz
	 */
	public IRegionBiz getRegionbiz() {
		return regionbiz;
	}
	/**
	 * @param regionbiz the regionbiz to set
	 */
	public void setRegionbiz(IRegionBiz regionbiz) {
		this.regionbiz = regionbiz;
	}	
	
}
